<!--
 * @Description: 寒假校园精英PK赛报名列表
 * @Author: Li Yujie
 * @Date: 2021/01/21
 * @Last Modified by: Li Yujie
 * @Last Modified time: 2021/08/10
-->
<template>
    <PageLayout title="用户参与列表">
        <template slot="filter">
            <div class="filter-item">
                <div class="filter-item__title">
                    活动期数
                </div>
                <el-select
                    v-model="filterData.index"
                    filterable
                    size="medium"
                    placeholder="请选择活动期数"
                    @change="getTableData(1)"
                >
                    <el-option
                        label="全部"
                        :value="-1"
                    />
                    <el-option
                        v-for="item in indexList"
                        :key="item.index"
                        :label="`第${item.index}期`"
                        :value="item.index"
                    />
                </el-select>
            </div>
            <div class="filter-item">
                <div class="filter-item__title">
                    学校名称
                </div>
                <el-cascader
                    v-model="filterData.school_name"
                    placeholder="请选择学校"
                    :options="school_tree"
                    filterable
                    clearable
                    :show-all-levels="false"
                    size="medium"
                    change-on-select
                    expand-trigger="click"
                    @change="getTableData(1)"
                    @keyup.native.enter="getTableData(1)"
                    :props="{
                        label: 'name',
                        value: 'name',
                        children: 'children',
                    }"
                />
            </div>
            <div class="filter-item">
                <div class="filter-item__title">
                    活动状态
                </div>
                <el-select
                    v-model="filterData.activity_status"
                    filterable
                    placeholder="全部"
                    size="medium"
                    @change="getTableData(1)"
                >
                    <el-option
                        v-for="item in statusList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                </el-select>
            </div>
            <div class="filter-item">
                <div class="filter-item__title">
                    用户信息
                </div>
                <el-input
                    placeholder="请输入用户信息"
                    v-model="filterData.keyword"
                    suffix-icon="el-icon-search"
                    clearable
                    size="medium"
                    @keydown.enter.native="getTableData(1)"
                />
            </div>
            <div class="filter-item">
                <div class="filter-item__title">
                    活动资格
                </div>
                <el-select
                    v-model="filterData.status"
                    filterable
                    placeholder="全部"
                    size="medium"
                    @change="getTableData(1)"
                >
                    <el-option
                        label="全部"
                        :value="99"
                    />
                    <el-option
                        label="未取消"
                        :value="1"
                    />
                    <el-option
                        label="取消资格"
                        :value="-1"
                    />
                    <el-option
                        label="淘汰"
                        :value="-2"
                    />
                </el-select>
            </div>
            <div class="filter-item">
                <div class="filter-item__title">
                    报名时间
                </div>
                <el-date-picker
                    v-model="dateTimeRange"
                    type="datetimerange"
                    range-separator="-"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    :default-time="['00:00:00', '23:59:59']"
                    size="medium"
                    style="width: 360px"
                    value-format="timestamp"
                />
            </div>
            <div class="filter-item">
                <el-button
                    type="primary"
                    size="small"
                    class="query-button"
                    @click="getTableData(1)"
                >
                    查询
                </el-button>
            </div>
        </template>
        <!--筛选-->
        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            ref="table"
            class="data-table"
            v-loading="tableLoading"
        >
            <el-table-column
                label="序号"
                align="center"
                width="80"
                type="index"
            />
            <el-table-column
                label="活动期数"
                align="center"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    第{{ scope.row.index }}期
                </template>
            </el-table-column>
            <el-table-column
                prop="true_name"
                label="用户昵称"
                show-overflow-tooltip
                align="left"
                :formatter="emptyFormatter"
            />
            <el-table-column
                label="精英等级"
                show-overflow-tooltip
                align="center"
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <div v-if="scope.row.agent_level !== -1">E{{ scope.row.agent_level }}</div>
                </template>
            </el-table-column>
            <el-table-column
                prop="phone"
                label="联系方式"
                show-overflow-tooltip
                align="center"
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="school_name"
                label="所在学校"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="activity_title"
                label="活动名称"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                label="用户报名时间"
                width="160"
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    {{ scope.row.apply_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm:ss') }}
                </template>
            </el-table-column>
            <el-table-column
                label="活动进度"
                show-overflow-tooltip
                align="center"
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <div class="status-wrapper">
                        <div
                            :class="['status-dot',scope.row.activity_status === 1 ? 'success' : 'normal']"/>
                        <div v-if="scope.row.activity_status === 1">进行中</div>
                        <div v-else-if="scope.row.activity_status === -1">已结束</div>
                        <div v-else>--</div>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                label="活动资格"
                show-overflow-tooltip
                align="center"
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <div class="status-wrapper">
                        <div
                            :class="['status-dot',scope.row.cancel_status === 1 ? 'success' : 'danger']"/>
                        <div v-if="scope.row.cancel_status === 1">未取消</div>
                        <div
                            v-if="scope.row.cancel_status === -1"
                            style="color: #F95B56">取消资格</div>
                        <div
                            v-if="scope.row.cancel_status === -2"
                            style="color: #F95B56">淘汰</div>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                label="活动时间"
                width="180"
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    {{ scope.row.activity_start_time * 1000 | dateFormatter('yyyy/MM/dd') }} - {{ scope.row.activity_end_time * 1000 | dateFormatter('yyyy/MM/dd') }}
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="left"
                width="180"
            >
                <template slot-scope="scope">
                    <div class="option-btn-group">
                        <el-button
                            type="text"
                            size="medium"
                            :disabled="scope.row.cancel_status === -2"
                            @click="handleSetStatus(scope.row, -2)"
                        >
                            淘汰
                        </el-button>
                        <el-button
                            type="text"
                            size="medium"
                            :disabled="scope.row.cancel_status !== 1"
                            @click="handleSetStatus(scope.row, -1)"
                        >
                            本档期资格取消
                        </el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- /分页 -->
        <template slot="pagination">
            <div/>
            <el-pagination
                layout="total, prev, pager, next, jumper"
                background
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </template>
        <!-- 确认移除弹窗 -->
        <el-dialog
            :title="set_status === -1 ? '取消资格' : '淘汰用户'"
            :visible.sync="dialogVisible"
            width="640px"
            :close-on-click-modal="false"
            class="reject-dialog"
            @close="reason = ''"
        >
            <div class="reject-tips">
                <div class="icon">
                    <i class="el-icon-warning"/>
                </div>
                <div class="content">
                    <div v-if="set_status === -1">本档期资格取消后，再次报名数据重新计算</div>
                    <div v-if="set_status === -2">淘汰后，该精英将不能参与本次活动</div>
                </div>
            </div>
            <div class="reject-title is-required">
                <span style="color: #FF3030">*</span>
                {{ set_status === -1 ? '取消资格' : '淘汰' }}原因
            </div>
            <el-input
                v-model="reason"
                type="textarea"
                :autosize="{ minRows: 4, maxRows: 6}"
                :placeholder="`请输入${set_status === -1 ? '取消资格' : '淘汰'}原因`"
                :maxlength="100"
                show-word-limit
            />
            <div class="dialog-footer">
                <el-button
                    size="medium"
                    type="default"
                    :style="{width: '100px', marginRight: '32px'}"
                    @click="dialogVisible = false"
                >
                    取消
                </el-button>
                <el-button
                    type="primary"
                    size="medium"
                    :style="{width: '100px'}"
                    :disabled="!reason"
                    @click="confirmSetStatus(apply_no, set_status, reason)"
                >
                    确认
                </el-button>
            </div>
        </el-dialog>
    </PageLayout>
</template>

<script>
import { back } from '@/mixin/back';

export default {
    name: 'ElitePkApplyList',
    mixins: [back],
    components: {},
    data() {
        return {
            filterData: {
                // 用户信息
                keyword: '',
                // 学校名称
                school_name: [],
                index: -1,
                // 活动状态
                activity_status: 99,
                // 是否被取消过资格
                status: 99,
                // 报名时间
                start_time: '',
                end_time: ''
            },
            tableData: [],
            tableLoading: false,
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            dateTimeRange: [],
            // 审核类型
            statusList: [
                {label: '全部', value: 99},
                {label: '进行中', value: 1},
                {label: '已结束', value: -1},
            ],
            school_tree: [],
            indexList: [],
            dialogVisible: false,
            reason: '',
            apply_no: '',
            set_status: ''
        };
    },
    computed: {},
    watch: {
        dateTimeRange(value) {
            this.filterData.start_time = value ? value[0] / 1000 : '';
            this.filterData.end_time = value ? value[1] / 1000 : '';
            this.getTableData(1);
        }
    },

    mounted() {
        this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '校园精英PK赛'}, {title: '用户参与列表'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
        this.getSchoolTree();
        this.getIndexList();
    },

    methods: {

        /**
         * @description 获取期数列表
         */
        getIndexList() {
            this.$post('/student/task_stuagent_extra_pk_activity%5Cactivity_config%5Cget_list_for_select', {}, res => {
                if (res.code === 1) {
                    this.indexList = res.data.list;
                } else {
                    this.$notify({
                        title: '提示',
                        message: res.msg,
                        type: 'warning'
                    });
                }
            });
        },

        /**
         * @description 获取省/市/学校三级联动
         */
        getSchoolTree () {
            this.$post('/student/local_info%5Cschool_tree',{},resp => {
                if (resp.code === 1) {
                    this.school_tree = resp.data.tree;
                }
            });
        },

        /**
         * @description 获取列表数据
         * @param page
         */
        getTableData(page) {
            this.tableLoading = true;
            let params = { ...this.filterData };

            params.page_id = page || this.page.now_page;
            params.school_name = params.school_name.length > 0 ? params.school_name[2]: '';

            this.$post('/student/task_stuagent_extra_pk_activity%5Cstudent_apply%5Cget_list', params, res => {
                if (res.code === 1) {
                    this.tableData = res.data.list;
                    this.page = res.data.page;
                } else {
                    this.$notify({
                        title: '提示',
                        message: res.msg,
                        type: 'warning'
                    });
                }
                this.tableLoading = false;
            });
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 资格取消
         * @param row
         * @param type - -1取消资格 -2淘汰
         */
        handleSetStatus(row, type) {
            if(!this.authResult.auth_cancel) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            this.apply_no = row.apply_no;
            this.set_status = type;
            this.dialogVisible = true;
        },

        /**
         * 确认淘汰
         * @param apply_no
         * @param status
         * @param cancel_reason
         */
        confirmSetStatus(apply_no, status, cancel_reason = '') {
            let params = {
                apply_no,
                status,
                cancel_reason,
            };

            this.$post('/student/task_stuagent_extra_pk_activity%5Cstudent_apply%5Cset_status', params, res => {
                if (res.code === 1) {
                    this.dialogVisible = false;
                    this.getTableData();
                    this.$notify({
                        title: '提示',
                        type: 'success',
                        message: '操作成功'
                    });
                } else {
                    this.$notify({
                        title: '提示',
                        type: 'warning',
                        message: res.msg
                    });
                }
            });
        }
    }
};
</script>

<style lang="less" scoped>
.reject-dialog {
    .reject-tips {
        display: flex;
        align-items: flex-start;
        margin-bottom: 24px;
        .icon {
            font-size: 16px;
            margin-right: 10px;
            margin-top: 3px;
            color: #F95B56;
        }
        .content {
            font-size: 14px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #666666;
            line-height: 22px;
        }
    }
    .reject-title {
        font-size: 14px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #333333;
        margin-bottom: 12px;
    }
    /deep/ .el-dialog__footer {
        text-align: center;
        padding: 0 0 30px;
        margin-top: 6px;
    }

    /deep/ .el-dialog__body {
        padding: 24px 64px;
    }
}

.dialog-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 24px;
}
</style>
